import 'package:boss/color_constant.dart';
import 'package:boss/models/commodity.dart';
import 'package:boss/provider/offers_provider.dart';
import 'package:boss/textStyle/text_style.dart';
import 'package:boss/theme/theme.dart';
import 'package:boss/ui/page/offersManagement/page/plusOffersMode/page/buyGiftOffers/provider/buy_gift_offers_provider.dart';
import 'package:boss/ui/page/offersManagement/page/plusOffersMode/page/orderDiscount/widget/offer_commodity_item.dart';
import 'package:boss/ui/page/offersManagement/page/plusOffersMode/page/widget/offers_form_field_common.dart';
import 'package:boss/ui/widget/button/fixed_bottom_btn.dart';
import 'package:boss/ui/widget/custom_divier.dart';
import 'package:boss/ui/widget/custom_list_title.dart';
import 'package:boss/ui/widget/label_button.dart';
import 'package:boss/ui/widget/picker/commodityPicker/commodity_picker.dart';

import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';

class BuyGiftOffersBrowser extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Expanded(
          child: SingleChildScrollView(
            padding: EdgeInsets.symmetric(
              vertical: 30.w,
            ),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                OffersFormFieldCommon<BuyGiftOffersProvider>(
                  title: '买赠',
                ),
                CustomDivider(
                  height: 20.w,
                ),
                Padding(
                  padding: EdgeInsets.symmetric(
                    horizontal: 55.w,
                  ),
                  child: Column(
                    children: <Widget>[
                      Consumer<BuyGiftOffersProvider>(
                        builder: (context, buyGiftOffersProvider, child) {
                          return TwoTextField(
                            initFirstValue: buyGiftOffersProvider.buyCount,
                            initSecendValue: buyGiftOffersProvider.giftCount,
                            onFirstChanged: (value) {
                              var buyGiftOffersProvider = Provider.of<BuyGiftOffersProvider>(
                                context,
                                listen: false,
                              );
                              buyGiftOffersProvider.setBuyCount(value);
                            },
                            onSecendChanged: (value) {
                              var buyGiftOffersProvider = Provider.of<BuyGiftOffersProvider>(
                                context,
                                listen: false,
                              );
                              buyGiftOffersProvider.setGiftCount(value);
                            },
                          );
                        },
                      ),
                      CustomListTitle(
                        title: Consumer<BuyGiftOffersProvider>(
                          builder: (context, buyGiftOffersProvider, _) {
                            return RichText(
                              text: TextSpan(text: '参与活动的菜品', style: TextStyleConstant().normal_1_28, children: [
                                TextSpan(
                                  text: '（${buyGiftOffersProvider.commoditys.length}）',
                                  style: TextStyleConstant().origin_24,
                                ),
                              ]),
                            );
                          },
                        ),
                        isHideDivider: true,
                        trailing: LabelButton(
                          title: '+选择菜品',
                          textStyle: TextStyleConstant().blue_24,
                          onTap: () async {
                            var buyGiftOffersProvider = Provider.of<BuyGiftOffersProvider>(
                              context,
                              listen: false,
                            );
                            var result = await showCommodityPicker(
                              context,
                              selectedCommodities: buyGiftOffersProvider.commoditys,
                              isShowSku: true,
                              isShowPackage: false,
                              filterType: [Commodity.COMMODITY_TYPE_PACKAGE, Commodity.COMMODITY_TYPE_WEIGH],
                            );
                            if (result != null) {
                              buyGiftOffersProvider.setCommoditys(result);
                            }
                          },
                        ),
                      ),
                      Consumer<BuyGiftOffersProvider>(
                        builder: (context, buyGiftOffersProvider, child) {
                          return ListView.builder(
                            shrinkWrap: true,
                            physics: NeverScrollableScrollPhysics(),
                            itemBuilder: (context, index) {
                              var current = buyGiftOffersProvider.commoditys[index];
                              return OfferCommodityItem(
                                data: current,
                                onDeleted: () {
                                  buyGiftOffersProvider.removeCommodity(current);
                                },
                              );
                            },
                            itemCount: buyGiftOffersProvider.commoditys.length,
                          );
                        },
                      ),
                      CustomListTitle(
                        title: Consumer<BuyGiftOffersProvider>(
                          builder: (context, buyGiftOffersProvider, _) {
                            return RichText(
                              text: TextSpan(text: '赠送的菜品', style: TextStyleConstant().normal_1_28, children: [
                                TextSpan(
                                  text: '（${buyGiftOffersProvider.giftCommoditys.length}）',
                                  style: TextStyleConstant().origin_24,
                                ),
                              ]),
                            );
                          },
                        ),
                        isHideDivider: true,
                        trailing: LabelButton(
                          title: '+选择菜品',
                          textStyle: TextStyleConstant().blue_24,
                          onTap: () async {
                            var buyGiftOffersProvider = Provider.of<BuyGiftOffersProvider>(
                              context,
                              listen: false,
                            );
                            var result = await showCommodityPicker(context,
                                selectedCommodities: buyGiftOffersProvider.giftCommoditys,
                                isShowSku: true,
                                isShowPackage: false,
                                filterType: [Commodity.COMMODITY_TYPE_PACKAGE, Commodity.COMMODITY_TYPE_WEIGH]);
                            if (result != null) {
                              buyGiftOffersProvider.setGiftCommoditys(result);
                            }
                          },
                        ),
                      ),
                      Consumer<BuyGiftOffersProvider>(
                        builder: (context, buyGiftOffersProvider, child) {
                          return ListView.builder(
                            shrinkWrap: true,
                            physics: NeverScrollableScrollPhysics(),
                            itemBuilder: (context, index) {
                              var current = buyGiftOffersProvider.giftCommoditys[index];
                              return OfferCommodityItem(
                                data: current,
                                onDeleted: () {
                                  buyGiftOffersProvider.removeGiftCommodity(current);
                                },
                              );
                            },
                            itemCount: buyGiftOffersProvider.giftCommoditys.length,
                          );
                        },
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
        FixedBottomBtn(
          title: '保 存',
          onTap: () async {
            var buyGiftOffersProvider = Provider.of<BuyGiftOffersProvider>(
              context,
              listen: false,
            );
            var offersProvider = Provider.of<OffersProvider>(
              context,
              listen: false,
            );

            var flag = await buyGiftOffersProvider.addBuyGiftOffers();
            if (flag) {
              offersProvider.backstageRefresh();

              Navigator.pop(context);
            }
          },
        ),
      ],
    );
  }
}

class TwoTextField extends StatefulWidget {
  final String initFirstValue;
  final String initSecendValue;
  final VoidCallback onDelete;
  final ValueChanged<String> onFirstChanged;
  final ValueChanged<String> onSecendChanged;

  const TwoTextField({
    Key key,
    this.initFirstValue,
    this.initSecendValue,
    this.onDelete,
    this.onFirstChanged,
    this.onSecendChanged,
  }) : super(key: key);

  @override
  _TwoTextFieldState createState() => _TwoTextFieldState();
}

class _TwoTextFieldState extends State<TwoTextField> {
  @override
  Widget build(BuildContext context) {
    return DefaultTextStyle(
      style: TextStyleConstant().normal_1_28,
      child: Theme(
        data: lightTheme.copyWith(
          inputDecorationTheme: InputDecorationTheme(
            hintStyle: TextStyleConstant().normal_4_28,
            border: NoInputBorder(),
            enabledBorder: NoInputBorder(),
            focusedBorder: NoInputBorder(),
          ),
        ),
        child: Container(
          decoration: BoxDecoration(
            border: Border(
              bottom: BorderSide(
                color: ColorConstant.e6e6,
                width: 1.px,
              ),
            ),
          ),
          child: Row(
            children: <Widget>[
              Text('买'),
              _FormField(
                onChanged: widget.onFirstChanged,
                initialValue: widget.initFirstValue,
              ),
              Text('赠'),
              _FormField(
                onChanged: widget.onSecendChanged,
                initialValue: widget.initSecendValue,
              ),
              Spacer(),
            ],
          ),
        ),
      ),
    );
  }
}

class _FormField extends StatelessWidget {
  final String initialValue;
  final ValueChanged<String> onChanged;

  const _FormField({
    Key key,
    this.onChanged,
    this.initialValue,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      child: TextFormField(
        keyboardAppearance: Brightness.light,
        initialValue: initialValue,
        onChanged: onChanged,
        textAlign: TextAlign.center,
        decoration: InputDecoration(
            hintText: '请输入',
            contentPadding: EdgeInsets.symmetric(
              vertical: 35.w,
            )),
        keyboardType: TextInputType.number,
        inputFormatters: [FilteringTextInputFormatter.allow(RegUtil.integerReg)],
      ),
      width: 180.w,
    );
  }
}
